package com.hopu.blb_erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopu.blb_erp.entity.Supplier;
import com.hopu.blb_erp.service.SupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Api
@RestController
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @ApiOperation("分页查询角色")
    @GetMapping("/supplier/{current}/{pageSize}")
    public ResponseEntity<Page> findPage(@PathVariable("current")Integer current, @PathVariable("pageSize")Integer pageSize){
        Page<Supplier> supplierPage = new Page<>(current, pageSize);
        Page<Supplier> page = supplierService.page(supplierPage);
        return ResponseEntity.ok(supplierPage);
    }

    @ApiOperation("模糊查询")
    @GetMapping("/supplier/like")
    public ResponseEntity<Page<Supplier>> findLikeSupplier(Integer current, Integer pageSize, String province, String city, String county, String name){
        QueryWrapper<Supplier> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(province),"address",province)
        .like(StringUtils.isNotBlank(city),"address",city)
        .like(StringUtils.isNotBlank(county),"address",county)
        .like(StringUtils.isNotBlank(name),"name",name);
        Page<Supplier> supplierPage = new Page<>(current, pageSize);
        Page<Supplier> page = supplierService.page(supplierPage,queryWrapper);
        return ResponseEntity.ok(page);
    }

    @ApiOperation("修改订单")
    @PutMapping("/supplier")
    public ResponseEntity<String> modifySupplier(@RequestBody Supplier supplier){
        supplierService.updateById(supplier);
        return ResponseEntity.ok("ok");
    }
    @ApiOperation("查找公司和产品")
    @GetMapping("/supplier/{id}")
    public ResponseEntity<Supplier> findSupplierById(@PathVariable("id")Integer id){
        Supplier supplier = supplierService.findSupplierById(Long.valueOf(id));

        return ResponseEntity.ok(supplier);
    }
}
